package com.igs.parse.query;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.loyin.ds.DBConnectionManager;

public class QueryCountUtil
{
	public static String dsName="igs";
	public static boolean isTodayDownLoad=false;//false;//今天是否已经分析下载网页
	public static double getDownLoadSizeCount(String userid)
	{
		double i=0;
		DBConnectionManager dbcm = null;
		try
		{
			dbcm = new DBConnectionManager(dsName);
			String queryRowSql="SELECT sum(downloadsize)/1024 as cm FROM igs_downloadurl";
			if(userid!=null)
				queryRowSql=queryRowSql+" where userid="+userid;
			PreparedStatement pstQueryRow = dbcm.getPrepstmt(queryRowSql);
			ResultSet rs = pstQueryRow.executeQuery();
			if(rs.next())
			{
				i=rs.getDouble("cm");
			}
			dbcm.closeDbconnection();
		}
		catch(Exception ex)
		{
			if(dbcm!=null)
				dbcm.closeDbconnection();
		}
		return i;
	}
	
	public static int getTodayDownLoadCount(String userid)
	{
		int i=0;
		DBConnectionManager dbcm = null;
		try
		{
			dbcm = new DBConnectionManager(dsName);
		    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//转换格式 
		    String todayStr = sdf.format(new Date());
			String queryRowSql="SELECT count(*) as c FROM igs_downloadurl "
				+"where userid="+userid+
				" and recordtime like '"+todayStr+"%' "
				;
			PreparedStatement pstQueryRow = dbcm.getPrepstmt(queryRowSql);
			ResultSet rs = pstQueryRow.executeQuery();
			if(rs.next())
			{
				i=rs.getInt("c");
			}
			dbcm.closeDbconnection();
		}
		catch(Exception ex)
		{
			if(dbcm!=null)
				dbcm.closeDbconnection();
		}
		return i;
	}
	
	public static List<Map<String,String>> getTodayUserMessageCount()
	{
		List<Map<String,String>> resultList=new ArrayList<Map<String,String>>();
		DBConnectionManager dbcm = null;
		try
		{
			dbcm = new DBConnectionManager(dsName);
		    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//转换格式 
		    String todayStr = sdf.format(new Date());
			String queryRowSql  =" select T.fullname,T.phone,count(T.urlid) as dlurlcount from "
								+" ( "
								+" SELECT usr.fullname,usr.phone,dlurl.id as urlid "
								+" FROM igs_downloadurl dlurl "
								+" left join igs_userinfo usr on dlurl.userid=usr.id "
								+" where dlurl.recordtime like '"+todayStr+"%' "
								+" )T "
								+" group by T.fullname,T.phone "
								;
			PreparedStatement pstQueryRow = dbcm.getPrepstmt(queryRowSql);
			ResultSet rs = pstQueryRow.executeQuery();
			while(rs.next())
			{
				Map<String,String> userMessageCountMap=new HashMap<String,String>();
				userMessageCountMap.put("fullname", rs.getString("fullname"));
				userMessageCountMap.put("phone", rs.getString("phone"));
				userMessageCountMap.put("dlurlcount", rs.getString("dlurlcount"));
				resultList.add(userMessageCountMap);
			}
			dbcm.closeDbconnection();
		}
		catch(Exception ex)
		{
			if(dbcm!=null)
				dbcm.closeDbconnection();
		}
		QueryCountUtil.isTodayDownLoad=false;
		return resultList;
	}
	
	
}
